package com.dataart.training.crimequalification.beans;

import com.dataart.training.crimequalification.facesutils.FacesMessageHandlingUtils;
import com.dataart.training.crimequalification.facesutils.ResourceBundleManager;
import com.dataart.training.crimequalification.service.RegistrationService;
import com.google.i18n.phonenumbers.PhoneNumberUtil;
import com.google.i18n.phonenumbers.Phonenumber;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Component;

import javax.faces.bean.ManagedBean;
import java.io.Serializable;
import java.util.ResourceBundle;

@ManagedBean
@Component
@Scope(value = "request")
public class RegistrationBean implements Serializable {

    // -----------------------------------------------------------------

    private static final Logger logger = LoggerFactory
            .getLogger(RegistrationBean.class);

    // -----------------------------------------------------------------

    private String username;
    private String password;
    private String name;
    private String email;
    private String phoneNumber;

    private String registrationResult;

    private boolean registrationComplete;

    // -----------------------------------------------------------------

    @Autowired
    private RegistrationService registrationService;

    // ------------------------------------------------------------------

    public String doRegister() {

        logger.info("Registering new user by username: " + username);

        try {

            PhoneNumberUtil phoneNumberUtil = PhoneNumberUtil.getInstance();
            Phonenumber.PhoneNumber phoneNumberEntity = phoneNumberUtil.parse(phoneNumber, "ZZ");
            phoneNumber = phoneNumberUtil.format(phoneNumberEntity, PhoneNumberUtil.PhoneNumberFormat.E164);
            registrationService.registerUser(username, password, name, email, phoneNumber);
            registrationComplete = true;
            // return "/views/index?faces-redirect=true";

        } catch (DataIntegrityViolationException e) {

            FacesMessageHandlingUtils facesMessageHandlingUtils = new FacesMessageHandlingUtils();
            facesMessageHandlingUtils.addMessageToComponent("registerForm",
                    "Error: user with such e-mail, phone number or username is already registered.");

            registrationResult = "registrationFailed";

            return null;

        } catch (Exception ex) {

            ResourceBundleManager resourceBundleManager = new ResourceBundleManager();
            ResourceBundle msgsBundle = resourceBundleManager.getResourceBundleByName("msgs");
            String message = resourceBundleManager.getValueFromBundleByKey(msgsBundle, "register.registrationfailure");

            FacesMessageHandlingUtils facesMessageHandlingUtils = new FacesMessageHandlingUtils();
            facesMessageHandlingUtils.addMessageToComponent("registerForm", message + ": " + "Internal error.");
            logger.error("Internal error." ,ex);

        }

        registrationResult = "successfulRegistration";

        return "/views/register";
    }

    public void initiateRegistration() {
        registrationResult = "initiatedRegistration";
    }

    // -----------------------------------------------------------------

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }


    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public boolean isRegistrationComplete() {
        return registrationComplete;
    }

    public void setRegistrationComplete(boolean registrationComplete) {
        this.registrationComplete = registrationComplete;
    }

    public String getRegistrationResult() {
        return registrationResult;
    }

}
